package com.juhuixing.common.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.juhuixing.common.exception.BusinessException;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.io.IOException;
import java.sql.Timestamp;

/**
 *@ClassName TimestampJsonSerializer
 *@Description TODO
 *@Author 刘猛
 *@Date 2019/3/20 14:08
 **/
public class TimestampJsonSerializer extends JsonSerializer<Timestamp> {
    @Override
    public void serialize(Timestamp timestamp, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
        try {
            jsonGenerator.writeString(DateFormatUtils.format(timestamp,"yyyy-MM-dd HH:mm:ss"));
        } catch (BusinessException e) {
            jsonGenerator.writeString(String.valueOf(timestamp.getTime()));
        }
    }
}
